var Util =
{
    //------------------------------    创建对象    ------------------------------
    createUIPanel: function (package, component, Parent) {
        var newPanel = fairygui.UIPackage.createObject(package, component).asCom;
        newPanel.name = component;
        if (window._view === undefined || window._view === null) {
            var _view = fairygui.GRoot.inst.addChild(newPanel);
            _view.setSize(fairygui.GRoot.inst.width, fairygui.GRoot.inst.height);
            window._view = _view;
            return newPanel;
        }
        if (Parent) {
            var ct = Parent;
        } else {
            var ct = window._view;
        }
        ct.addChild(newPanel);
        newPanel.setSize(ct.width, ct.height);
        newPanel.addRelation(ct, fairygui.RelationType.Size);
        return newPanel;
    },
    //------------------------------    销毁对象    ------------------------------
    diestoryPanel: function (panel) {
        panel.removeFromParent();   //从父对象销毁
    },
    //------------------------------    销毁子对象   ------------------------------
    diestorySonPanel: function (panel, sonPanel) {
        panel.removeChild(sonPanel);
        return null;
    },
    //------------------------------    Time   ------------------------------
    dateFormat: function (fmt, date) { //author: meizz
        var o = {
            "M+": date.getMonth() + 1,                 //月份
            "d+": date.getDate(),                    //日
            "h+": date.getHours(),                   //小时
            "m+": date.getMinutes(),                 //分
            "s+": date.getSeconds(),                 //秒
            "q+": Math.floor((date.getMonth() + 3) / 3), //季度
            "S": date.getMilliseconds()             //毫秒
        };
        if (/(y+)/.test(fmt))
            fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    },
    dateFormatYMDHMS: function (date) {
        return this.dateFormat("yyyy-MM-dd hh:mm:ss", date);
    },
    dateFormatYMD: function (date) {
        return this.dateFormat("yyyy-MM-dd ", date);
    },
    dateFormatMDHMS: function (date) {
        return this.dateFormat("MM-dd hh:mm:ss", date);
    },
    _dateFormatYMD: function (date) {
        return this.dateFormat("yyyyMMdd ", date);
    },
    getNowTimDYNext: function (number) {
        var nowDate = new Date();
        var preDate = new Date(nowDate.getTime() - (24 * 60 * 60 * 1000) * number); //前一天
        var month = preDate.getMonth() + 1 < 10 ? "0" + (preDate.getMonth() + 1) : preDate.getMonth() + 1;
        var day = preDate.getDate() < 10 ? "0" + preDate.getDate() : preDate.getDate();
        var year = preDate.getFullYear();
        return [month, day, year];
    },
    //------------------------------    获取文件后缀   ------------------------------
    getCaption: function (obj) {
        var index = obj.lastIndexOf("\.");
        obj = obj.substring(index + 1, obj.length);
        return obj;
    },
    //------------------------------    获取文件路径   ------------------------------
    getFilePath: function (list) {
        var res_list = new Array();
        var len = list.length;
        if (!len) return;
        for (var i = 0; i < len; i++) {
            var url = list[i] + '?v=' + appConfig.updateVersion;
            var type = null;
            var fileSuffix = Util.getCaption(list[i]);
            switch (fileSuffix) {
                case 'fui': type = Loader.BUFFER; break;
                case 'png':
                case 'jpg': type = Loader.IMAGE; break;
                case 'mp3': type = Loader.BUFFER; break;
            }
            res_list.push({ url, type });
        }
        return res_list;
    },
    cacheResource: function (list) {
        var len = list.length;
        if (!len) {
            return;
        }
        for (var i = 0; i < len; i++) {
            var url = list[i] + '?v=' + appConfig.updateVersion;
            var data = Laya.loader.getRes(url);
            Laya.loader.cacheRes(list[i], data);
        }
    },
    getCurrentWebDir: function () {
        var url = window.location.href;
        var pos = url.lastIndexOf('/');
        if (pos > 0) {
            url = url.substr(0, pos);
        }
        url = url.replace("h5_hall_kevin", "");
        url = url.replace("h5_fk_hall", "");
        return url;
    },
    //获取 url ? 后面字符串
    GetRequest: function () {
        var url = location.search;
        var request = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                request[strs[i].split("=")[0]] = strs[i].split("=")[1];
            }
        }
        return request;
    },
    checkUrl: function (imgurl) {
        if (imgurl == null) {
            return "";
        }
        var url = imgurl;
        if (typeof url == "string") {
            if (url.indexOf("resource") == 0) {
                url = window.location.origin + "/" + url;
            }
        }
        if (url == Number(url)) {
            url = window.location.origin + "/resource/" + url + ".png"
        }
        if (url.indexOf(".png") < 0 && url.indexOf(".jpg") < 0) {
            url = url + "?type=a.jpg";
        }
        return url;
    },
    checkAccount: function (account) {
        if (account == null) {
            return "";
        }
        var str = account;
        if (typeof str == "string") {
            if (str.indexOf("wx") == 0) {
                if (userInfo.getUserData().phone) {
                    str = userInfo.getUserData().phone;
                } else {
                    str = '';
                }
            }
        }
        return str;
    },
    //------------------------------------------
    isNative: function () {
        window.isNative = false;
        try {
            if (JSandOC) {
                window.isNative = true;
                return true;
            }
        } catch (e) {
            return false
        }
        return false
    },
    IsNative: function () {
        if (window.isNative == true) return true;
        window.isNative = false;
        try {
            if (JSandOC) {
                window.isNative = true;
                return true;
            }
        } catch (e) {
            return false
        }

        return false
    },
    drawLoadPro: function (obj, degree) {
        if (!obj) {
            return;
        }
        var LoadPro = obj.getChild('LoadPro');
        LoadPro.visible = true;

        LoadPro.image.fillAmount = 1 - degree;
    },
    getUriFromGameType: function (gt) {

        if (gt === "fkzjh") {
            return "h5_fkzjh";
        }

        if (gt === "fkniuniu17") {
            return "h5_fkniuniu17";
        }

        if (gt === "fkbj") {
            return "h5_fkblackjack";
        }

        if (gt === "fkdz") {
            return "h5_fkdz";
        }

        if (gt === "fkmj") {
            return "h5_fkmj";
        }
        if (gt === "fk_cymj") {
            return "h5_cymj";
        }
        if (gt === "fkpj") {
            return "h5_fkpj";
        }
        if (gt === "fkanbao") {
            return "h5_anbao";
        }
        return "";
    },
    getGameType: function (info) {
        var gameType = '';
        switch (info.gt) {
            case 'fkniuniu17':
                gameType = '牛牛';
                if (info.data.guiNum) {
                    gameType = '癞子牛牛';
                }
                break;
            case 'fkmj': gameType = '麻将'; break;
            case 'fk_cymj': gameType = '崇阳麻将'; break;
            case 'fkpj':
                gameType = '牌九';
                if (info.data.playMode === 2) {
                    gameType = '大话牌九';
                }
                break;
            case 'fkzjh': gameType = '金花'; break;
            case 'blackjack': gameType = '21点'; break;
            case 'dezhou': gameType = '德州'; break;
        }
        return gameType;
    },
    getMjCard: function (data) {
        var info = [];
        info.cids = [];
        for (var i = 0; i < 34; i++) {
            for (var j = 0; j < data.cids[i]; j++) {
                info.cids.push(i);
            }
        }
        info.gang = [];
        for (var i = 0; i < data.gang.length; i++) {
            for (var j = 0; j < 4; j++) {
                info.gang.push(data.gang[i]);
            }
        }
        info.hu = [];
        for (var i = 0; i < data.hu.length; i++) {
            info.hu.push(data.hu[i]);
        }
        info.peng = [];
        for (var i = 0; i < data.peng.length; i++) {
            for (var j = 0; j < 3; j++) {
                info.peng.push(data.peng[i]);
            }
        }
        return info;
    },
    //------------------------------    输入限制 : 仅数字   ------------------------------
    limitInput: function (obj) {
        obj = this;
        if (obj) {
            obj.text = parseInt(obj.text);
            if (isNaN(obj.text)) {
                obj.text = '';
            }
        }
    },
    //------------------------------    获取验证码的 倒计时
    countdown_Code: function (obj) {
        var time = 60;
        obj.grayed = false;
        obj.enabled = false;
        obj.title = time + 's';
        var delaytimer = setInterval(function () {
            time--;
            obj.title = time + 's';
            if (time <= 0) {
                obj.enabled = true;
                obj.title = '获取验证码';
                clearInterval(delaytimer);
            }
        }.bind(this), 1000);
    },
    checkPwd: function (text) {
        var str = /^[a-zA-Z0-9.?]+$/g;
        return str.test(text);
    },
    //-------------------------------   复制
    initCopyBtn: function (btnObj, str, btnName) {
        var layaContainer = Laya.Browser.getElementById("layaContainer");
        var _btnCopyWxNumber = Laya.Browser.getElementById(btnName);
        if (!_btnCopyWxNumber) return;
        _btnCopyWxNumber.innerHTML = '';
        _btnCopyWxNumber.style.position = "absolute";
        _btnCopyWxNumber.style.color = '#ffffff';
        _btnCopyWxNumber.style["opacity"] = '0';

        Laya.Utils.fitDOMElementInArea(_btnCopyWxNumber, btnObj._container, 0, 0, btnObj.width, btnObj.height);
        layaContainer.appendChild(_btnCopyWxNumber);
        if (btnName === "Numbering") {
            Observer.fire('getPasteText', { msg: str }); //粘贴内容
        }
        if (btnObj.clipboard) {
            btnObj.clipboard.destroy();
        }
        btnObj.clipboard = new ClipboardJS('.' + btnName, {
            text: function (trigger) {
                if (btnObj.show) {
                    Tip.common('复制成功');
                    return str;
                }
                Observer.fire('copySur', '复制成功'); //复制成功,发消息进行后续操作
                Util.removeCopyBtn(btnName);
                return str;
            }
        })
    },
    removeCopyBtn: function (btnName) {
        _btnCopyWxNumber = Laya.Browser.getElementById(btnName);
        _btnCopyWxNumber.innerHTML = '';
        _btnCopyWxNumber.style.left = "-5000px";
        console.log('清除复制按钮: ' + btnName);
    },
    removeCopyBtnAll: function () {
        console.log('清除所有复制按钮');
        Util.removeCopyBtn('copy0');
        Util.removeCopyBtn('copy1');
        Util.removeCopyBtn('copy2');
        Util.removeCopyBtn('copy3');
        Util.removeCopyBtn('copy4');
        Util.removeCopyBtn('copy5');
        Util.removeCopyBtn('copyShare');
        Util.removeCopyBtn('copyID');
        Util.removeCopyBtn('btn_L');
        Util.removeCopyBtn('Numbering');
    },
    isJSON: function (str) {
        if (typeof str == 'string') {
            try {
                var obj = JSON.parse(str);
                if (typeof obj == 'object' && obj) {
                    return true;
                } else {
                    return false;
                }

            } catch (e) {
                console.log('error：' + str + '!!!' + e);
                return false;
            }
        }
        console.log('It is not a string!')
    },
    changeScreenMode: function (mode) {
        switch (mode) {
            case "vertical":
                window.stopAdapt = true;
                Laya.stage.scaleMode = "noscale";
                Laya.stage.screenMode = "vertical";
                var newHeight = Laya.stage.width;
                var newWidth = Laya.stage.height
                //Laya.init(newWidth, newHeight, Laya.WebGL);
                Laya.stage.width = newWidth;
                Laya.stage.height = newHeight;
                Laya.stage.setScreenSize(newWidth, newHeight);
                break;
            case "horizontal":
                window.stopAdapt = false;
                Laya.stage.scaleMode = "fixedheight";
                Laya.stage.screenMode = "horizontal";
                var newHeight = Laya.stage.width;
                var newWidth = Laya.stage.height
                //Laya.init(newWidth, newHeight, Laya.WebGL);
                Laya.stage.width = newWidth;
                Laya.stage.height = newHeight;
                Laya.stage.setScreenSize(newWidth, newHeight);
                break;
        }
    },
    inputShow: function (obj, text) {
        if (!laya.utils.Browser.onMobile) {
            return;
        }
        console.log('手机模式:禁用输入!')
        obj.promptText = '';
        obj.editable = false;
        obj.on(laya.events.Event.CLICK, this, function () {
            var tmptext = obj.text;
            obj.text = '';
            Observer.regist('inputSur', function (args) {
                console.log('inputSur', tmptext)
                if (args.msg === null || args.msg === undefined || args.msg.length === 0) {
                    obj.text = tmptext;
                    return;
                }
                obj.text = args.msg;
            })
            appConfig.inputPanel.show(text, obj);
        });
    }, //竖屏输入框
    inputNum: function (obj) {
        if (!laya.utils.Browser.onMobile) {
            return;
        }
        console.log('手机模式:禁用输入!')
        obj.promptText = '';
        obj.editable = false;
        obj.on(laya.events.Event.CLICK, this, function () {
            var tmptext = obj.text;
            obj.text = '';
            new Panel_InputNumber('text');
            Observer.regist('Number', function (args) {
                if (args === null || args === undefined || args.length === 0) {
                    obj.text = tmptext;
                    return;
                }
                obj.text = args;
                Observer.remove('Number');
            });
        });
    }, //竖屏输入框
    objEditable: function (obj, value) {
        obj.editable = value;
    },
    checkLogin: function () {
        var param = Util.GetRequest();
        if (param["backHall"] == "club") {
            new FriendCircleScene();
            Util.removeCopyBtnAll();
        }
    },
}

Date.prototype.format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}